<?php
/*
 * 1626cms 文件上傳
|   @param: $dir      -- 存放目錄,最后加"/" [字串] 
|   @param: $file_var -- 表單變量 [字串] 
|   @param: $max_size -- 設定最大上傳值,以k為單位. [整數/浮點數] 
|   @param: $type     -- 限定后輟名(小寫)，多個用"/"隔開,不限定則留空 [字串] 
|   @param: $name     -- 上傳后命名,留空則為原名,true為系統隨機定名 [布林值] 
|   return: 上傳后文件名
 * ============================================================================
 * 版權所有: 1626網絡，并保留所有權利。
 * 網站地址: http://www.1626.com；
 * ----------------------------------------------------------------------------
 * 這不是一個自由軟件！您只能在不用于商業目的的前提下對程序代碼進行修改和
 * 使用；不允許對程序代碼以任何形式任何目的的再發布。
 * ============================================================================
*/
function _asUpFiles($dir, $file_var, $max_size = '', $type = '', $name = false) {
	if (! file_exists ( $dir ))
		showmsg ( "上傳圖片失敗：上傳目錄 " . $dir . " 不存在!", 0 );
	if (! is_writable ( $dir )) {
		showmsg ( "上傳圖片失敗：上傳目錄 " . $dir . " 無法寫入!", 0 );
		exit ();
	}
	$upfile = & $_FILES ["$file_var"];
	$upfilename = $upfile ['name'];
	if (! ($upfilename === '')) {
		if (! is_uploaded_file ( $upfile ['tmp_name'] )) {
			showmsg ( '上傳圖片失敗：你選擇的文件無法上傳', 0 );
			exit ();
		}
		if ($max_size > 0 && $upfile ['size'] / 1024 > $max_size) {
			showmsg ( "上傳圖片失敗：文件大小不能超過  " . $max_size . "KB", 0 );
			exit ();
		}
		$ext_name = strtolower ( str_replace ( ".", "", strrchr ( $upfilename, "." ) ) );
		if (! ($type === '') && strpos ( $type, $ext_name ) === false) {
			showmsg ( "上傳圖片失敗：只允許上傳 " . $type . " 的文件！", 0 );
			exit ();
		}
		($name == true) ? $uploadname = time () . mt_rand ( 100, 999 ) . "." . $ext_name : '';
		($name == false) ? $uploadname = $upfilename : '';
		! is_bool ( $name ) ? ($uploadname = $name . "." . $ext_name) : '';
		//$uploadname = $name ? md5(uniqid(rand())).".".$ext_name : $upfilename; 
		if (! move_uploaded_file ( $upfile ['tmp_name'], $dir . $uploadname )) {
			showmsg ( '上傳圖片失敗：文件上傳出錯！', 0 );
			exit ();
		}
		return $uploadname;
	} else {
		return '';
	}
}
/*圖象縮略函數
參數說明：
$srcfile 原圖地址； 
$dir  新圖目錄 
$thumbwidth 縮小圖寬最大尺寸 
$thumbheitht 縮小圖高最大尺寸 
$ratio 默認等比例縮放 為1是縮小到固定尺寸。 
*/
function makethumb($srcfile, $dir, $thumbwidth, $thumbheight, $ratio = 0) {
	//判斷文件是否存在 
	if (! file_exists ( $srcfile ))
		return false;
	
		//生成新的同名文件，但目錄不同 
	$imgname = explode ( '/', $srcfile );
	$arrcount = count ( $imgname );
	$dstfile = $dir . $imgname [$arrcount - 1];
	//縮略圖大小 
	$tow = $thumbwidth;
	$toh = $thumbheight;
	if ($tow < 40)
		$tow = 40;
	if ($toh < 45)
		$toh = 45;
	
		//獲取圖片信息 
	$im = '';
	if ($data = getimagesize ( $srcfile )) {
		if ($data [2] == 1) {
			$make_max = 0; //gif不處理 
			if (function_exists ( "imagecreatefromgif" )) {
				$im = imagecreatefromgif ( $srcfile );
			}
		} elseif ($data [2] == 2) {
			if (function_exists ( "imagecreatefromjpeg" )) {
				$im = imagecreatefromjpeg ( $srcfile );
			}
		} elseif ($data [2] == 3) {
			if (function_exists ( "imagecreatefrompng" )) {
				$im = imagecreatefrompng ( $srcfile );
			}
		}
	}
	if (! $im)
		return '';
	$srcw = imagesx ( $im );
	$srch = imagesy ( $im );
	$towh = $tow / $toh;
	$srcwh = $srcw / $srch;
	if ($towh <= $srcwh) {
		$ftow = $tow;
		$ftoh = $ftow * ($srch / $srcw);
	} else {
		$ftoh = $toh;
		$ftow = $ftoh * ($srcw / $srch);
	}
	if ($ratio) {
		$ftow = $tow;
		$ftoh = $toh;
	}
	//縮小圖片 
	if ($srcw > $tow || $srch > $toh || $ratio) {
		if (function_exists ( "imagecreatetruecolor" ) && function_exists ( "imagecopyresampled" ) && @$ni = imagecreatetruecolor ( $ftow, $ftoh )) {
			imagecopyresampled ( $ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch );
		} elseif (function_exists ( "imagecreate" ) && function_exists ( "imagecopyresized" ) && @$ni = imagecreate ( $ftow, $ftoh )) {
			imagecopyresized ( $ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch );
		} else {
			return '';
		}
		if (function_exists ( 'imagejpeg' )) {
			imagejpeg ( $ni, $dstfile );
		} elseif (function_exists ( 'imagepng' )) {
			imagepng ( $ni, $dstfile );
		}
	} else {
		//小于尺寸直接復制 
		copy ( $srcfile, $dstfile );
	}
	imagedestroy ( $im );
	if (! file_exists ( $dstfile )) {
		return '';
	} else {
		return $dstfile;
	}
}
?>
